<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/5/29
 * Time: 20:00
 */

namespace app\model;


class HomePage extends Model
{
    public function __construct($db)
    {
        parent::__construct($db, "user_material");
    }

    /**
     * 替换地址
     */
    //    public function update_url(){
    //        $this->db->select("*");
    //        $this->db->from("quality_video");
    //        $query =  $this->db->get();
    //        $datas = $query->all();
    //        return $datas;
    //    }
    //
    //    public function updates_url($table,$datas,$where){
    //       return $this->db->update($table,$datas,$where);
    //    }



    /**
     * @param $page
     * @param $pageSize
     * 首页精选列表
     */
    public function home_page_index($page,$pageSize){
        $this->db->select("a.id,a.uuid,a.video_uuid,a.pic,a.duration,a.video_url,a.video_desc,a.join_type_sub,a.is_dub,a.quality,a.add_time,a.nick_name,a.nick_pic,a.width2,a.height2,a.first_pic3,a.share_pic,a.count_material_love,a.count_material_share,a.is_wangchun,b.sub_title");
        $this->db->from("user_material as a");
        $this->db->join("type_sub as b","a.join_type_sub=b.id");
        $this->db->where(["del1"=>2,"examine"=>3,"quality"=>2]);
        //        $this->db->where(["examine"=>3,"quality"=>2]);
        $total =  $this->db->count_all_results();
        $this->db->order_by(["quality_order"=>"asc","add_time"=>"desc"]);
        $this->db->limit($pageSize,($page-1)*$pageSize);
        $query =  $this->db->get();
        $datas = $query->all();
        $data["total"] = $total;
        $data["data"] = $datas;
        //        echo $this->db->last_query();exit;
        return $data;
        //        return $this->db;

    }

    /**
     * 查询排序为1和2的精选视频
     */
    public function quality(){
        $this->db->select("a.id,a.uuid,a.video_uuid,a.pic,a.duration,a.video_url,a.video_desc,a.join_type_sub,a.is_dub,a.quality,a.add_time,a.nick_name,a.nick_pic,a.width2,a.height2,a.first_pic3,a.share_pic,a.count_material_love,a.count_material_share,a.is_wangchun,b.sub_title");
        $this->db->from("user_material as a");
        $this->db->join("type_sub as b","a.join_type_sub=b.id");
        $this->db->where(["del1"=>2,"examine"=>3,"quality"=>2,"quality_order<="=>2]);
        //        $this->db->where(["examine"=>3,"quality"=>2,"quality_order<="=>2]);
        $total =  $this->db->count_all_results();
        $this->db->order_by(["quality_order"=>"asc"]);
        $query =  $this->db->get();
        $datas = $query->all();
        $data["total"] = $total;
        $data["data"] = $datas;
        //        echo $this->db->last_query();exit;
        return $data["data"];
    }

    /**
     * 查询50精选视频
     */
    public function quality1($time,$page,$pageSize,$small_selection=""){
        $this->db->select("a.id,a.uuid,a.video_uuid,a.pic,a.duration,a.video_url,a.video_desc,a.join_type_sub,a.is_dub,a.quality,a.add_time,a.nick_name,a.nick_pic,a.width2,a.height2,a.first_pic3,a.share_pic,a.count_material_love,a.count_material_share,a.is_wangchun,b.sub_title");
        $this->db->from("user_material as a");
        $this->db->join("type_sub as b","a.join_type_sub=b.id");
        if(empty($small_selection)){
            $this->db->where(["del1"=>2,"examine"=>3,"quality"=>2,"quality_order>"=>2]);
            //            $this->db->where(["examine"=>3,"quality"=>2,"quality_order>"=>2]);
        }else{
            $this->db->where(["del1"=>2,"examine"=>3,"quality"=>2,"quality_order>"=>2,"a.id<"=>$small_selection]);
            //            $this->db->where(["examine"=>3,"quality"=>2,"quality_order>"=>2,"id<"=>$small_selection]);
        }
        $total =  $this->db->count_all_results();
        $this->db->order_by(["a.id"=>"desc","a.add_time"=>"desc"]);
        $this->db->limit($pageSize);
        $query =  $this->db->get();
        $datas = $query->all();
        $data["total"] = $total;
        $data["data"] = $datas;
        //        echo $this->db->last_query();exit;
        return $data["data"];
    }

    /**
     * 查询150条普通视频，是精选的三倍
     */
    public function ordinary($time,$page,$pageSize,$small_ordinary=""){
        $this->db->select("a.id,a.uuid,a.video_uuid,a.pic,a.duration,a.video_url,a.video_desc,a.join_type_sub,a.is_dub,a.quality,a.add_time,a.nick_name,a.nick_pic,a.width2,a.height2,a.first_pic3,a.share_pic,a.count_material_love,a.count_material_share,a.is_wangchun,b.sub_title");
        $this->db->from("user_material as a");
        $this->db->join("type_sub as b","a.join_type_sub=b.id");
        if(empty($small_ordinary)){
            $this->db->where(["del1"=>2,"examine"=>3,"quality"=>1]);
            //            $this->db->where(["examine"=>3,"quality"=>1]);
        }else{
            //            $this->db->where(["dels"=>2,"examine"=>3,"quality"=>1,"id<"=>$small_ordinary]);
            $this->db->where(["examine"=>3,"quality"=>1,"id<"=>$small_ordinary]);
        }

        $total =  $this->db->count_all_results();
        $this->db->order_by(["id"=>"desc","add_time"=>"desc"]);
        if(empty($small_ordinary)){
            $this->db->limit($pageSize,($page-1)*$pageSize);
        }else{
            $this->db->limit($pageSize,0);
        }

        $query =  $this->db->get();
        $datas = $query->all();
        $data["total"] = $total;
        $data["data"] = $datas;
        //        echo $this->db->last_query();exit;
        return $data["data"];
    }

    /**
     * @param $page
     * @param $pageSize
     * 首页分类视频列表
     */
    public function type_video($page,$pageSize,$type_id){
        $this->db->select("a.id,a.uuid,a.video_uuid,a.pic,a.duration,a.video_url,a.video_desc,a.join_type_sub,a.is_dub,a.add_time,a.nick_name,a.nick_pic,a.width2,a.height2,a.first_pic3,a.share_pic,a.count_material_love,a.count_material_share,a.is_wangchun,b.sub_title");
        $this->db->from("user_material as a");
        $this->db->join("type_sub as b","a.join_type_sub=b.id");
        $this->db->where(["del1"=>2,"examine"=>3,"join_type"=>$type_id]);
        //        $this->db->where(["examine"=>3,"join_type"=>$type_id]);
        $total =  $this->db->count_all_results();
        $this->db->order_by(["quality_order"=>"asc","add_time"=>"desc"]);
        $this->db->limit($pageSize,($page-1)*$pageSize);
        $query =  $this->db->get();
        $datas = $query->all();
        $data["total"] = $total;
        $data["data"] = $datas;
        //        echo $this->db->last_query();exit;
        return $data;
        //        return $this->db;

    }

    /**
     * 查询点赞了哪些视频
     */
    public function zan($uuid){
        $this->db->select("video_uuid,other_uuid,other_id");
        $this->db->from("material_zan");
        $this->db->where(["my_uuid"=>$uuid]);
        $this->db->order_by(["id"=>"desc"]);
        $query =  $this->db->get();
        $datas = $query->all();
        $data["data"] = $datas;
        //        echo $this->db->last_query();exit;
        return $data["data"];
    }

    /**
     * 查询关注了哪些视频
     */
    public function guanzu($uuid){
        $this->db->select("video_uuid,other_uuid");
        $this->db->from("material_follow");
        $this->db->where(["my_uuid"=>$uuid]);
        $this->db->order_by(["id"=>"desc"]);
        $query =  $this->db->get();
        $datas = $query->all();
        $data["data"] = $datas;
        //        echo $this->db->last_query();exit;
        return $data["data"];
    }
}